package com.oocl.scan.handler;

import com.oocl.scan.jdt.parser.ModifierParser;
import com.oocl.scan.model.Component;
import com.oocl.scan.model.JavaClassElement;
import com.oocl.scan.model.Relationship;
import com.oocl.scan.store.ComponentStore;
import com.oocl.scan.store.RelationshipStore;
import com.oocl.scan.utils.ScanConstants;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.eclipse.jdt.core.dom.SingleMemberAnnotation;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class BaseHandler implements Handler{

  Logger logger = LoggerFactory.getLogger(BaseHandler.class);

  void addToComponentStore(Component component) {
    ComponentStore.getInstance().add(component);
    logger.info(component.toString());
    System.out.println(component);
    if(ComponentStore.getInstance().getCount() >= 500){
      ComponentStore.getInstance().saveComponent();
      ComponentStore.getInstance().setComponentQueue(new ConcurrentLinkedQueue<>());
    }
  }

  void addToRelationshipStore(Relationship relationship) {
    RelationshipStore.getInstance().add(relationship);
    logger.info(relationship.toString());
    System.out.println(relationship);
    if(RelationshipStore.getInstance().getCount() >= 500){
      RelationshipStore.getInstance().saveRelationship();
      RelationshipStore.getInstance().setRelationshipQueue(new ConcurrentLinkedQueue<>());
    }
  }

  boolean isController(JavaClassElement element) {
    TypeDeclaration typeDeclaration = element.getTypeDeclaration();
    SingleMemberAnnotation classRequestMappingAnnotation = ModifierParser.getSingleMemberAnnotation(typeDeclaration, ScanConstants.ANNOTATION_REQUEST_MAPPING);
    if (classRequestMappingAnnotation != null) {
      return true;
    }
    return false;
  }

  boolean isDao(String className, String fullClassName) {
    if (className.toLowerCase().contains("dao") || className.toLowerCase().contains("daoimpl") || fullClassName.contains("." + ScanConstants.DAO_CLASS + ".")) {
      return true;
    }
    return false;
  }

  public void shutdown(){

  }
}
